DNN_Eval.py

### Copyright (C) 2020-2023  Alessio Gianelle, INFN Padova
###
### This program is free software: you can redistribute it and/or modify
### it under the terms of the GNU General Public License as published by
### the Free Software Foundation, either version 3 of the License, or
### later version.
###
### This program is distributed in the hope that it will be useful,
### but WITHOUT ANY WARRANTY; without even the implied warranty of
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
### GNU General Public License for more details.
###
### You should found a copy of the GNU General Public License
### at this path: https://www.gnu.org/licenses/ .

# coding: utf-8
import sys
import os
import gc
# import keras
from keras.models import load_model
# extra
import pandas as pd
from joblib import dump, load

### These to limit the GPU memory resources used by keras ###

import tensorflow as tf

# TensorFlow wizardry for GPU dynamic memory allocation
config = tf.compat.v1.ConfigProto()

# Don't pre-allocate memory; allocate as-needed
config.gpu_options.allow_growth = True

# Only allow a fraction of the GPU memory to be allocated
config.gpu_options.per_process_gpu_memory_fraction = 0.5

# Create a session with the above options specified.
tf.compat.v1.keras.backend.set_session(tf.compat.v1.Session(config=config))

#############################################################


# Remove warning messages from commands output
def warn(*args, **kwargs):
    pass

import warnings
warnings.warn = warn

# first argoment is the configuration file
conf = __import__(sys.argv[1])

testpath = conf.testpath
evalpath = conf.evalpath

selectfeatures = conf.features
# if the second argoment is set used reduced features
if len(sys.argv) == 3:
  selectfeatures = conf.reducedFeatures
  testpath += "Red"
  evalpath += "Red"

# create test directory
if not os.path.exists(evalpath):
    os.makedirs(evalpath)

basepath = "%s/%s"%(conf.datapath, conf.datadir)

for namePopA in conf.namePopA:
  res = []
  for namePopB in conf.namePopB:
    name = "%s-%s"%(namePopA, namePopB)
    rawdata = pd.read_hdf("%s/Co_%s.hdf"%(basepath, name))
    data = rawdata[selectfeatures]
    model = load_model("%s/Model_%s.h5"%(testpath, name))
    scaler = load('%s/Scaler_%s.gz'%(testpath, name))
    scaledata = scaler.transform(data)
    probabilities = model.predict(scaledata)
    data = pd.concat([rawdata, pd.DataFrame(probabilities, columns=['probabilities'])], axis=1)
    res.append(data)
  with pd.ExcelWriter('%s/Co_%s_out.xlsx'%(evalpath, name)) as writer:
    for pb in range(len(conf.namePopB)):
      res[pb].to_excel(writer, sheet_name='%s'%conf.namePopB[pb])




